package com.houseliker.adapter.calculate;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.houseliker.R;
import com.houseliker.adapter.BaseListAdapter;
import com.houseliker.model.LoanDetail;

import java.util.List;

/**
 * Created by sunrx on 2015/12/24.
 */
public class LoanListAdapter extends BaseListAdapter<LoanDetail> {
    public LoanListAdapter(Context context, List<LoanDetail> list) {
        super(context, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_loandetail, null);
            holder.tv_year = (TextView) convertView.findViewById(R.id.tv_year);
            holder.ll_detail = (LinearLayout) convertView.findViewById(R.id.ll_detail);
            holder.tv_month = (TextView) convertView.findViewById(R.id.tv_month);
            holder.tv_payment = (TextView) convertView.findViewById(R.id.tv_payment);
            holder.tv_payment_principal = (TextView) convertView.findViewById(R.id.tv_payment_principal);
            holder.tv_payment_interest = (TextView) convertView.findViewById(R.id.tv_payment_interest);
            holder.tv_surplus = (TextView) convertView.findViewById(R.id.tv_surplus);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        LoanDetail detail = list.get(position);
        if ("1".equals(detail.month)) {
            holder.tv_year.setVisibility(View.VISIBLE);
            holder.tv_year.setText(detail.year);
        } else {
            holder.tv_year.setVisibility(View.GONE);
        }
        holder.tv_month.setText(detail.month);
        holder.tv_payment.setText(detail.payment);
        holder.tv_payment_principal.setText(detail.payment_principal);
        holder.tv_payment_interest.setText(detail.payment_interest);
        holder.tv_surplus.setText(detail.surplus);
        if (1 == detail.state) {
            holder.ll_detail.setBackgroundResource(R.color.grayE5);
        } else {
            holder.ll_detail.setBackgroundResource(R.color.white);
        }

        return convertView;
    }

    class ViewHolder {
        TextView tv_year;
        LinearLayout ll_detail;
        TextView tv_month;
        TextView tv_payment;
        TextView tv_payment_principal;
        TextView tv_payment_interest;
        TextView tv_surplus;
    }
}
